package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.News;
import cn.wolfcode.trip.base.domain.NewsContent;
import cn.wolfcode.trip.base.domain.StrategyContent;
import cn.wolfcode.trip.base.domain.News;
import cn.wolfcode.trip.base.mapper.NewsContentMapper;
import cn.wolfcode.trip.base.mapper.NewsMapper;
import cn.wolfcode.trip.base.query.NewsQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.INewsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class NewsServiceImpl implements INewsService {
    @Autowired
    private NewsMapper newsMapper;
    @Autowired
    private NewsContentMapper newsContentMapper;


    @Override
    public int deleteByPrimaryKey(Long id) {
        return newsMapper.deleteByPrimaryKey(id);
    }


    @Override
    public News selectByPrimaryKey(Long id) {
        return newsMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<News> selectAll() {
        return newsMapper.selectAll();
    }


    @Override
    public PageInfo<News> query(NewsQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<News> list = newsMapper.selectForList(qo);
        return new PageInfo<News>(list);
    }

    @Override
    public NewsContent getNewsContentById(Long id) {
        return newsContentMapper.selectByPrimaryKey(id);
    }
    @Override
    public void saveOrUpdate(News news) {
        // 如果没有设置序号，就自增
        if (news.getSequence() == null) {
            int sequence = newsMapper.selectMaxSequence();
            news.setSequence(sequence + 1);
        }
        // 如果是发布状态就设置发布时间
        if (news.getState() == News.STATE_PUBLIC) {
            if ( news.getReleaseTime()==null){
                news.setReleaseTime(new Date());
            }
        }

        // 内容

        // 根据有没有id判断是增加还是修改
        if(news.getId()!=null){
            NewsContent content = news.getNewsContent();
            newsMapper.updateByPrimaryKey(news);
            content.setId(news.getId());
            System.out.println(content.getId());
            newsContentMapper.updateByPrimaryKey(content);
        }else{
            newsMapper.insert(news);
            NewsContent content = news.getNewsContent();
            content.setId(news.getId());
            newsContentMapper.insert(content);
        }
    }
    //更新浏览量
    @Override
    public void updateBroseNumById(Long id,Integer browseNum) {
        newsMapper.updateBroseNumById(id,browseNum);
    }
    //更新点赞数据
    @Override
    public void updateLikeNumById(Long id, Integer likeNum) {
        newsMapper.updateLikeNumById(id,likeNum);
    }

    @Override
    public News selectNewsByUserId(Long userId,Long newsId) {
        return newsMapper.selectNewsByUserId(userId,newsId);
    }

    @Override
    public void insertUserAndCollectionRelation(Long newsId, Long userId) {
        newsMapper.insertUserAndCollectionRelation(newsId,userId);
    }

    @Override
    public void updateCollectionNumById(Long id, Integer collectionNum) {
        newsMapper.updateCollectionNumById(id,collectionNum);
    }

    @Override
    public void deleteCollectionAndUserRelation(Long newsId, Long userId) {
        newsMapper.deleteCollectionAndUserRelation(newsId,userId);
    }
}
